package com.ouman.luoliluoli.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.ouman.luoliluoli.MeiziDetailActivity;
import com.ouman.luoliluoli.R;
import com.ouman.luoliluoli.models.GankMeiziModel;

import java.util.List;

/**
 * Created by jintian on 12/05/2017.
 */

public class MeiziRecycleViewAdapter extends RecyclerView.Adapter<MeiziRecycleViewAdapter.ViewHolder> {


    private Context context;
    private List<GankMeiziModel> data;
    public MeiziRecycleViewAdapter(Context context, List<GankMeiziModel> data){
        this.context = context;
        this.data = data;
    }

    public MeiziRecycleViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View view = LayoutInflater.from(context).inflate(R.layout.recyclerview_item_meizi, null);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("===>>click!!!!!");
            }
        });

        return new MeiziRecycleViewAdapter.ViewHolder(view);
    }

    public void onBindViewHolder(final MeiziRecycleViewAdapter.ViewHolder holder, final int position){
        String imageUrlString = data.get(position).getUrl();

        Animation animation = AnimationUtils.loadAnimation(context, R.anim.anim_card_show);
        holder.meiziCardView.startAnimation(animation);

        Glide.clear(holder.meiziImageView);
        Glide.with(holder.meiziImageView.getContext())
                .load(imageUrlString)
                .asBitmap()
                .error(R.drawable.normal_bk_gray_green)
                .into(holder.meiziImageView);

        holder.meiziCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(), MeiziDetailActivity.class);
                intent.putExtra("imageUrl", data.get(position).getUrl());
                view.getContext().startActivity(intent);
                System.out.println("这是 " + data.get(position).getWho());

            }
        });

    }

    public int getItemCount(){
        return data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView meiziImageView;
        CardView meiziCardView;
        public ViewHolder(View view){
            super(view);
            meiziImageView = (ImageView) view.findViewById(R.id.imageViewMeizi);
            meiziCardView = (CardView) view.findViewById(R.id.cardViewMeizi);
        }

    }
}
